/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client.channel;

import me.l1k3.fx.client.channel.impl.BackgroundPositionChannelImplJS;
import me.l1k3.fx.client.channel.inter.Channel;
import me.l1k3.fx.client.channel.inter.ChannelContext;
import me.l1k3.fx.client.channel.trait.TraitDecorator;
import me.l1k3.fx.client.engine.inter.FXEngine;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;

public abstract class BackgroundPositionChannel implements Channel {
    protected boolean backward;

    protected boolean accumulate;

    protected TraitDecorator x = new TraitDecorator();

    protected TraitDecorator y = new TraitDecorator();

    public BackgroundPositionChannel() {
        x.setUnit("px");
        y.setUnit("px");
    }

    // - - - factory

    public final static BackgroundPositionChannel create(String type) {
        if (type.equals(FXEngine.JS)) {
            return new BackgroundPositionChannelImplJS();
        }

        return GWT.create(BackgroundPositionChannel.class);
    }

    public final static BackgroundPositionChannel range(String type, int startX, int endX, int startY, int endY, boolean accumulate) {
        BackgroundPositionChannel channel = create(type);
        channel.setXRange(startX, endX);
        channel.setYRange(startY, endY);
        channel.setAccumulate(accumulate);

        return channel;
    }

    public final static BackgroundPositionChannel byIncrement(String type, int diffX, int diffY) {
        BackgroundPositionChannel channel = create(type);
        channel.setXDiff(diffX);
        channel.setYDiff(diffY);
        channel.setAccumulate(true);

        return channel;
    }

    // - - -

    public final void setXDiff(int diff) {
        x.set(diff);
    }

    public final void setYDiff(int diff) {
        y.set(diff);
    }

    public final void setXRange(int start, int end) {
        x.set(start, end);
    }

    public final void setYRange(int start, int end) {
        y.set(start, end);
    }

    public final void setAccumulate(boolean accumulate) {
        this.accumulate = accumulate;
    }

    protected void setPosition(Style style, boolean backward) {
        style.setProperty("backgroundPosition", x.getString(backward) + " " + y.getString(backward));
    }

    // - - -

    @Override
    public boolean isForward() {
        return !backward;
    }

    @Override
    public void init(ChannelContext context) {
        setPosition(context.getStyle(), backward);
    }

    @Override
    public void toggle(ChannelContext context) {
        if (!accumulate) {
            backward = !backward;
            setPosition(context.getStyle(), backward);
        }
    }

    /*
     * @Override public void start(ChannelContext context) { }
     */

    /*
     * @Override public void update(ChannelContext context) { }
     */

    @Override
    public void complete(ChannelContext context) {
        if (accumulate) {
            x.accumulate();
            y.accumulate();
        }

        toggle(context);
    }
}
